package cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.logisModul.controller.admin.logisticsmodul;

import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.logisModul.controller.admin.logisticsmodul.vo.*;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.logisModul.convert.logisticsmodul.LogisticsModulConvert;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.logisModul.dal.dataobject.logisticsmodul.AreaDo;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.logisModul.dal.dataobject.logisticsmodul.LogisticsModulDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.logisModul.dal.mysql.logisticsmodul.AreaMapper;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.logisModul.dal.mysql.logisticsmodul.LogisticsModulMapper;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.logisModul.service.logisticsmodul.LogisticsModulService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 运费模版")
@RestController
@RequestMapping("/qd/logistics-modul")
@Validated
public class LogisticsModulController {

    @Resource
    private LogisticsModulService logisticsModulService;
    @Resource
    AreaMapper areaMapper;
    @Resource
    LogisticsModulMapper logisticsModulMapper;

    @GetMapping("getCityList")
    @Operation(summary = "获取所有省")
    public CommonResult getCityList(){
        List<String> list=new ArrayList<>();
        for (AreaDo dto : areaMapper.selectList(new LambdaQueryWrapper<AreaDo>().eq(AreaDo::getLevelType, 1))) {
            list.add(dto.getName());
        }
        return success(list);
    }

    @GetMapping("getModelList")
    @Operation(summary = "获取运费模版下拉选")
    public CommonResult getModelList(){
        List<IdNameVo> list=new ArrayList<>();
        for (LogisticsModulDO vo : logisticsModulMapper.selectList()) {
            list.add(new IdNameVo(vo.getId(), vo.getName()));
        }
        return success(list);
    }

    @PostMapping("/create")
    @Operation(summary = "创建运费模版")
    @PreAuthorize("@ss.hasPermission('qd:logistics-modul:create')")
    public CommonResult<Integer> createLogisticsModul(@Valid @RequestBody LogisticsModulCreateReqVO createReqVO) {
        return success(logisticsModulService.createLogisticsModul(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新运费模版")
    @PreAuthorize("@ss.hasPermission('qd:logistics-modul:update')")
    public CommonResult<Boolean> updateLogisticsModul(@Valid @RequestBody LogisticsModulUpdateReqVO updateReqVO) {
        logisticsModulService.updateLogisticsModul(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除运费模版")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qd:logistics-modul:delete')")
    public CommonResult<Boolean> deleteLogisticsModul(@RequestParam("id") Integer id) {
        logisticsModulService.deleteLogisticsModul(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得运费模版")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qd:logistics-modul:query')")
    public CommonResult<LogisticsModulRespVO> getLogisticsModul(@RequestParam("id") Integer id) {
        LogisticsModulDO logisticsModul = logisticsModulService.getLogisticsModul(id);
        return success(LogisticsModulConvert.INSTANCE.convert(logisticsModul));
    }

    @GetMapping("/list")
    @Operation(summary = "获得运费模版列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('qd:logistics-modul:query')")
    public CommonResult<List<LogisticsModulRespVO>> getLogisticsModulList(@RequestParam("ids") Collection<Integer> ids) {
        List<LogisticsModulDO> list = logisticsModulService.getLogisticsModulList(ids);
        return success(LogisticsModulConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得运费模版分页")
    @PreAuthorize("@ss.hasPermission('qd:logistics-modul:query')")
    public CommonResult<PageResult<LogisticsModulRespVO>> getLogisticsModulPage(@Valid LogisticsModulPageReqVO pageVO) {
        PageResult<LogisticsModulDO> pageResult = logisticsModulService.getLogisticsModulPage(pageVO);
        return success(LogisticsModulConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出运费模版 Excel")
    @PreAuthorize("@ss.hasPermission('qd:logistics-modul:export')")
    @OperateLog(type = EXPORT)
    public void exportLogisticsModulExcel(@Valid LogisticsModulExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<LogisticsModulDO> list = logisticsModulService.getLogisticsModulList(exportReqVO);
        // 导出 Excel
        List<LogisticsModulExcelVO> datas = LogisticsModulConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "运费模版.xls", "数据", LogisticsModulExcelVO.class, datas);
    }

}
